﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BootyBay.Models;

namespace BootyBay.Repository
{
    public class ApplicationRepository
    {
        //create singlton instance
        static ApplicationRepository<View_Application> _instance = new ApplicationRepository<View_Application>();
        public static ApplicationRepository<View_Application> Instance()
        {
            return _instance;
        }
    }
    public class ApplicationRepository<T> : RepositoryBase<T> where T : View_Application
    {
        public IQueryable<View_Application> GetAllApplications()
        {
            var result = SqlQuery("select * from AllApplication").AsQueryable();

            return result;
        }

        public IQueryable<View_Application> GetAllValidApplications()
        {
            var result = GetAllApplications()
                .Where(a => !a.IsHidden);

            return result;
        }

        public IQueryable<View_Application> GetAllApplications(string keyword)
        {
            var result = GetAllApplications()
                .Where(a => a.ApplicationName.ToLower().Contains(keyword) || (a.Keywords != null && a.Keywords.ToLower().Contains(keyword)));

            return result;
        }

        public View_Application GetApplicationById(int applicationId)
        {
            return GetAllApplications().Where(a => a.Id == applicationId).FirstOrDefault();
        }

        public IQueryable<ApplicationProcessorArchitecture> GetUploadedPackages(int appId)
        {
            var versionId = GetApplicationById(appId).Id_ApplicationVersion;
            var packages = AppArchitectureRepository.Instance().GetByVersionId(versionId);
            return packages;
        }

        /// <summary>
        /// TODO. need better query.
        /// </summary>
        /// <param name="applicationVersionInfoId"></param>
        /// <returns></returns>

    }
}
